import { getIndustryList, getCountryList, getAuditStatus, getOrgType, getSpecialityList, getReturnMechanismList, getRoleSelect, getPPPRoleSelect, getPPPOrgType, getProjectStage } from '~/apis/components/selectApi';

type ConfigApi = (...p: any) => Promise<any>;

interface ConfigItem {
  api?: ConfigApi
  /** 初始化请求 */
  init: boolean
  /** 自定义 keyName */
  keyName: string
  /** 自定义 valueName */
  valueName: string
  /** 自定义请求参数 */
  params: unknown
  /** 是否开启 ajax 搜索模式 */
  ajaxSearch: boolean
  /** ajaxSearch: true 时，搜索参数名 */
  ajaxSearchKey: string
}

type ValidKeys = (
  'default'
  | 'industryList'
  | 'countryList'
  | 'auditStatus'
  | 'orgType'
  | 'specialityList'
  | 'returnMechanismList'
  | 'role'
  | 'pppRole'
  | 'pppOrgType'
  | 'projectStage'
);

const defaultConfig = {
  init: false,
  keyName: 'id',
  valueName: 'ename', // TODO: i18n 默认显示英文
  params: null,
  ajaxSearch: false,
  ajaxSearchKey: 'search',
};

const config: Record<ValidKeys, ConfigItem> = {
  // 默认模糊搜索
  default: {
    ...defaultConfig,
  },

  // 机构状态 institution status
  auditStatus: {
    ...defaultConfig,
    api: getAuditStatus,
    init: true,
    valueName: 'note',
  },

  // 国家 Country/Region
  countryList: {
    ...defaultConfig,
    api: getCountryList,
    init: true,
  },

  // 行业 industry
  industryList: {
    ...defaultConfig,
    api: getIndustryList,
    init: true,
  },

  // 机构类别 institution type
  orgType: {
    ...defaultConfig,
    api: getOrgType,
    init: true,
    valueName: 'note',
  },

  // 专业 main business
  specialityList: {
    ...defaultConfig,
    api: getSpecialityList,
    init: true,
  },

  // 回访机制 return Mechanism
  returnMechanismList: {
    ...defaultConfig,
    api: getReturnMechanismList,
    init: true,
    valueName: 'note',
  },

  // 用户角色 role
  role: {
    ...defaultConfig,
    api: getRoleSelect,
    init: true,
    valueName: 'name',
  },

  // 用户角色 pppRole(包括ppp管理员)
  pppRole: {
    ...defaultConfig,
    api: getPPPRoleSelect,
    init: true,
    valueName: 'name',
  },

  // 机构类别 pppOrgType(包括ppp管理员)
  pppOrgType: {
    ...defaultConfig,
    api: getPPPOrgType,
    init: true,
    valueName: 'note',
  },

  // 项目阶段
  projectStage: {
    ...defaultConfig,
    api: getProjectStage,
    init: true,
    valueName: 'note',
  },
};

export type ValidConfigKeys = keyof typeof config;

const getConfig = (type: ValidConfigKeys = 'default') => ({
  ...config.default,
  ...config[type],
});

export { getConfig };
